<?php

namespace Sms;

use app\admin\model\Config;
use app\admin\model\SmsSetting;
use Siam\Sms\AliSms;
use Siam\Sms\TencentSms;
use think\facade\Log;

class SmsSend
{
    private $config = null;

    /**
     * 发送短信
     * @param mixed $mobile 可发送多个手机号，变量为数组即可，如：[11111111111, 22222222222]
     * @param string $scene 场景值
     * @param array $params 模板参数
     * @return false|void
     * @throws \Exception
     */
    public function send($mobile, string $scene = '', array $params = [])
    {
        if (empty($scene) && empty($mobile)) {
            throw new \Exception('请求参数缺失');
        }
        //获取短信驱动
        $type = Config::where('name', 'sms_type')->value('value');
        $sms_config = SmsSetting::where('name', $type)->find();
        if ($sms_config) {
            $sms_config = $sms_config->toArray();
            if ($sms_config['template']) {
                $template = array_column($sms_config['template'], null, 'key');
            }
            $sms_config['template'] = $template;
            $this->config = $sms_config;
        }
        switch ($type) {
            case 'alisms':
                $this->aliSend($mobile, $scene, $params);
                break;
            case 'qcloudsms':
                $this->tencentSend($mobile, $scene, $params);
                break;
            case 'hwsms':
                break;
            default:
                throw new \Exception('短信驱动未配置');
        }
    }

    /**
     * 发送腾讯云短信
     * @param $mobile //手机号
     * @param string $scene 场景值
     * @param array $params 模板请求参数 腾讯云模板变量为索引数组，当你传入关联数组时会按顺序变为索引数组，如：['name' => '张三', 'code' => '123'] => ['张三', '123']
     * @return void
     * @throws \Exception
     */
    public function tencentSend($mobile, string $scene, array $params = [])
    {
        $key = $this->config['key'];
        $secret = $this->config['secret'];
        // 腾讯云短信发送短信需要指定应用id
        $appid = $this->config['appid'];
        $sign = $this->config['sign'];
        $template = !empty($this->config['template'][$scene]['value']) ? $this->config['template'][$scene]['value'] : '';
        if (empty($template)) {
            throw new \Exception('短信模板未配置');
        }
        $sms = new TencentSms($key, $secret);
        // 需要注意，设置配置不分先后顺序，send后也不会清空配置
        $result = $sms->setAppid($appid)->setMobile($mobile)->setTemplate($template)->setSign($sign)->setParams($params)->send();
        /**
         * 返回值为bool，你可获得腾讯云响应做出你业务内的处理
         *
         * status bool 此变量是此包用来判断是否发送成功
         * code string 腾讯云短信响应代码
         * message string 腾讯云短信响应信息
         */
        if (!$result) {
            $response = $sms->getResponse();
            Log::error("发送短信失败", $response);
        }
    }

    /**
     * 发送阿里云短信
     * @param $mobile //手机号
     * @param string $scene 场景值
     * @param array $params 阿里云模板变量为键值对数组，如['code' => rand(1000, 9999)]
     * @return void
     * @throws \Exception
     */
    public function aliSend($mobile, string $scene, array $params = [])
    {
        $key = $this->config['key'];
        $secret = $this->config['secret'];
        $sign = $this->config['sign'];
        $template = !empty($this->config['template'][$scene]['value']) ? $this->config['template'][$scene]['value'] : '';
        if (empty($template)) {
            throw new \Exception('短信模板未配置');
        }
        $sms = new AliSms($key, $secret);
        // 需要注意，设置配置不分先后顺序，send后也不会清空配置
        $result = $sms->setMobile($mobile)->setTemplate($template)->setSign($sign)->setParams($params)->send();
        /**
         * 返回值为bool，你可获得阿里云响应做出你业务内的处理
         *
         * status bool 此变量是此包用来判断是否发送成功
         * code string 阿里云短信响应代码
         * message string 阿里云短信响应信息
         */
        if (!$result) {
            $response = $sms->getResponse();
            Log::error("发送短信失败", $response);
        }
    }
}
